package com.giser.testapp.lib.Location;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.text.format.DateFormat;

import com.amap.api.location.AMapLocation;
import com.giser.testapp.lib.App.EnvApplication;
import com.giser.testapp.lib.Tool.TimeUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Lss on 2017/11/29.
 */

public class LocationConverter {

    public static GeographicLocation toGeographicLocation(Context context, Location location, Location lastLocation) {
        GeographicLocation gLoc = new GeographicLocation();
        if (location != null) {
            String locationTime = DateFormat.format("yyyy-MM-dd  HH:mm:ss.SSSZ", location.getTime()).toString();
            String currentTime = null;
            if (lastLocation != null) {
                currentTime = DateFormat.format("yyyy-MM-dd  HH:mm:ss.SSSZ", lastLocation.getTime()).toString();
                lastLocation = location;
            } else {
                lastLocation = location;
            }
            // 获取当前详细地址
            StringBuffer sb = new StringBuffer();
            if (lastLocation != null) {
                Geocoder gc = new Geocoder(context);
                List<Address> addresses = null;
                try {
                    addresses = gc.getFromLocation(lastLocation.getLatitude(), lastLocation.getLongitude(), 1);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if (addresses != null && addresses.size() > 0) {
                    Address address = addresses.get(0);
                    sb.append(address.getCountryName() + address.getLocality());
                    sb.append(address.getSubThoroughfare());
                }
            }

            // 获取经度
            gLoc.setLongitude(location.getLongitude());
            // 获取纬度
            gLoc.setLatitude(location.getLatitude());
            // 高度信息
            gLoc.setAltitude(location.getAltitude());
            // 位置提供者
            gLoc.setProvider(location.getProvider());
            // 方向角
            gLoc.setBearing(location.getBearing());
            // 速度 米/秒
            gLoc.setSpeed(location.getSpeed());
            // 位置的准确性
            gLoc.setAccuracy(location.getAccuracy());
            //您所在的城市
            gLoc.setAddress(sb.toString());
            //最新获取时间
            gLoc.setTime(locationTime);
            //上次获取时间
            gLoc.setLastTime(currentTime);
            gLoc.setUserID(EnvApplication.getApplication().userID);
            gLoc.setPowerValue(EnvApplication.getApplication().powerValue);
            EnvApplication.getApplication().lastLocation = location;
        }
        return gLoc;
    }

    public static GeographicLocation androdiToGeographicLocation(Context context, Location location, GeographicLocation lastLocation) {
        GeographicLocation gLoc = new GeographicLocation();
        if (location != null) {
            // 获取经度
            gLoc.setLongitude(location.getLongitude());
            // 获取纬度
            gLoc.setLatitude(location.getLatitude());
            // 高度信息
            gLoc.setAltitude(location.getAltitude());
            // 位置提供者
            gLoc.setProvider(location.getProvider());
            // 方向角
            gLoc.setBearing(location.getBearing());
            // 速度 米/秒
            gLoc.setSpeed(location.getSpeed());
            // 位置的准确性
            gLoc.setAccuracy(location.getAccuracy());
            Geocoder gc = new Geocoder(context);
            List<Address> addresses = null;
            try {
                addresses = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String addressStr=null;
            if (addresses != null && addresses.size() > 0) {
                Address address = addresses.get(0);
                addressStr=address.getCountryName()+" "+address.getLocality();
            }
            //您所在的城市
            gLoc.setAddress(addressStr);
            //最新获取时间
            gLoc.setTime(TimeUtil.formatUTC(location.getTime(),"yyyy-MM-dd HH:mm:ss"));
            //上次获取时间
            gLoc.setLastTime(null!=lastLocation?lastLocation.getTime():"");
            gLoc.setUserID(EnvApplication.getApplication().userID);
            gLoc.setPowerValue(EnvApplication.getApplication().powerValue);
        }
        return gLoc;
    }

    public static GeographicLocation aMapToGeographicLocation(AMapLocation location, GeographicLocation lastLocation) {
        GeographicLocation gLoc = new GeographicLocation();
        if (null !=location ) {
            GCJ2WGS gcj2WGS=new GCJ2WGS();
            HashMap<String, Double> hm =gcj2WGS.delta(location.getLatitude(),location.getLongitude());
            // 获取经度
            gLoc.setLongitude(hm.get("lon"));
            // 获取纬度
            gLoc.setLatitude(hm.get("lat"));

            // 获取经度
            /*Loc.setLongitude(location.getLongitude());
            // 获取纬度
            gLoc.setLatitude(location.getLatitude());*/
            // 高度信息
            gLoc.setAltitude(location.getAltitude());
            // 位置提供者
            gLoc.setProvider(location.getProvider());
            // 方向角
            gLoc.setBearing(location.getBearing());
            // 速度 米/秒
            gLoc.setSpeed(location.getSpeed());
            // 位置的准确性
            gLoc.setAccuracy(location.getAccuracy());
            //您所在的城市
            gLoc.setAddress(location.getCity() +" "+ location.getAddress());
            //最新获取时间
            gLoc.setTime(TimeUtil.formatUTC(location.getTime(),"yyyy-MM-dd HH:mm:ss"));
            //上次获取时间
            gLoc.setLastTime(null!=lastLocation?lastLocation.getTime():"");
            gLoc.setUserID(EnvApplication.getApplication().userID);
            gLoc.setPowerValue(EnvApplication.getApplication().powerValue);
        }
        return gLoc;
    }
}
